import java.util.Arrays;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int len = in.nextInt();
            int p = in.nextInt();
            in.nextLine();
            int[] nums = new int[len];
            int min = 1000001;
            int max = 0;
            int[] hash = new int[1000001];
            for (int i = 0; i < len; i++) {
                nums[i] = in.nextInt();
                hash[nums[i]]++;
                min = Math.min(min, nums[i]);
                max = Math.max(max, nums[i]);
            }
            Arrays.sort(nums);
            // prev[i]表示nums[i]之前有多少个元素
            // next[i]表示nums[i]之后有多少个元素
            // 那么nums[i]之前和之后差在p之间的元素的个数有
            int[] prev = new int[1000001];
            int[] next = new int[1000001];
            int count = 0;
            for (int i = min; i <= max; i++) {
                count += hash[i];
                prev[i] = count;
            }
            count = 0;
            for (int i = max; i >= min; i--) {
                count += hash[i];
                next[i] = count;
            }
            int maxLength = 0;
            for (int i = 0; i < len; i++) {
                int prevLength = i-p < 0 ? prev[nums[i]] : prev[nums[i]]-prev[nums[i-p]];
                int nextLength = i+p > 1000001 ? next[nums[i]] : next[nums[i]]-next[nums[i+p]];
                maxLength = Math.max(maxLength, prevLength+nextLength);
            }
            System.out.println(maxLength);
        }
    }
}
